package it.koutifaris.travianmap.view.distances;

import it.koutifaris.travianmap.geometry.DistanceCalculator;
import it.koutifaris.travianmap.info.Troop;
import it.koutifaris.travianmap.view.distances.DistancesView.DistancesItemView;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

class DistancesAdapter extends ArrayAdapter<Troop> {

	private final DistancesView view;
	private final DistanceCalculator calculator;

	public DistancesAdapter(Context context, DistancesView view, List<Troop> troops, DistanceCalculator calculator) {
		super(context, android.R.layout.simple_list_item_1);
		this.view = view;
		this.calculator = calculator;

		for (Troop troop : troops) {
			this.add(troop);
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View androidView = view.newItemView(parent);

		Troop troop = this.getItem(position);

		DistancesItemView viewItem = view.itemViewFor(androidView);
		viewItem.setTroop(troop.name());
		viewItem.setTime(calculator.getTime(troop.speed()));

		return androidView;
	}
}
